﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Data;
using Matrix.Common.Utility;
using Matrix.MaKaYunShang.Model;
using Matrix.MaKaYunShang.DAL;
using Matrix.MaKaYunShang.Model.Dto;
using Matrix.MaKaYunShang.Model.Enum;

namespace Matrix.MaKaYunShang.BLL
{
    public partial class PaymentRecordsBLL : BaseBLL<PaymentRecordsBLL>
    {
        #region 默认方法
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(PaymentRecords model)
        {
            return PaymentRecordsDAL.Instance.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(PaymentRecords model)
        {
            return PaymentRecordsDAL.Instance.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {
            return PaymentRecordsDAL.Instance.Delete(Id);
        }
        /// <summary>
        /// 批量删除一批数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            return PaymentRecordsDAL.Instance.DeleteList(Idlist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public PaymentRecords GetModel(int Id)
        {
            var ds = PaymentRecordsDAL.Instance.GetModel(Id);
            return ds == null ? null : ServicesHelper.DataSetToIList<PaymentRecords>(ds).ToList()[0];
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<PaymentRecords> GetModelList(string strWhere = null)
        {
            var ds = PaymentRecordsDAL.Instance.GetList(strWhere);
            return ServicesHelper.DataSetToIList<PaymentRecords>(ds).ToList();
        }
        #endregion

        #region 扩展方法

        /// <summary>
        ///     获得分页数据列表
        /// </summary>
        public TableDataViewDto<ViewPaymentRecordsDto> GetPagingList(int draw, int pageIndex, int pageSize, string searchKeyWord, string colOrder)
        {
            try
            {
                int totalCount = 0;
                List<ViewPaymentRecordsDto> list = PaymentRecordsDAL.Instance.GetPagingList(pageIndex, pageSize, searchKeyWord, colOrder, ref totalCount);
                list.ForEach(a =>
                {
                    a.Action =
                          "<a class='btn btn-warning' href='javascript:;' onclick=GetOrderById(" + a.OrderId + ");>查看关联订单</a> ";
                    a.PayResultStr = a.PayResult == (int)CommonEnum.PayResult.PayFail ? "支付失败" : a.PayResult == (int)CommonEnum.PayResult.PaySuccess
                        ? "支付成功" : a.PayResult == (int)CommonEnum.PayResult.ToBePaid ? "未支付" : "";
                    a.AddTimeStr = a.AddTime.ToString("yyyy-MM-dd  HH:mm");
                    var payPlatform = PayPlatformBLL.Instance.GetModel(a.PayPlatformId);
                    a.PayPlatformIdStr = payPlatform != null ? payPlatform.PayPlatformName : "";
                });
                var returnDataList = new TableDataViewDto<ViewPaymentRecordsDto>
                {
                    data = list,
                    draw = draw,
                    recordsFiltered = totalCount,
                    recordsTotal = totalCount
                };

                return returnDataList;
            }
            catch (Exception ex)
            {
                _fileLog.Error(" GetPagingList | Exception: " + ex);
                return null;
            }
        }

        /// <summary>
        /// 更新订单支付数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool UpdateByOrderIdAndLocalTransId(PaymentRecords model)
        {
            return PaymentRecordsDAL.Instance.UpdateByOrderIdAndLocalTransId(model);
        }
        #endregion
    }
}